package com.zhuanzhuan.hero.user.facade.label;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.zhuanzhuan.hero.user.dto.AddUserGroupLabelDTO;
import com.zhuanzhuan.hero.user.dto.DelUserGroupLabelDTO;
import com.zhuanzhuan.hero.user.dto.RelateUserGroupLabelDTO;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.helper.ZzLockHelper;
import com.zhuanzhuan.hero.user.service.IUserGroupService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.label.UserGroupLabelPageVO;
import com.zhuanzhuan.hero.user.vo.label.UserGroupLabelVO;
import com.zhuanzhuan.zzlock.lock.ZZEtcdLock;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @ClassName UserGroupFacadeImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/26 10:57
 */
@ServiceBehavior
@Slf4j
public class UserGroupFacadeImpl implements IUserGroupFacade {

    @Resource
    private IUserGroupService userGroupService;

    @Resource
    private ZzLockHelper lockHelper;

    @ZZMethod(value = "queryUserGroupLabels", desc = "查询分组标签", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserGroupLabelPageVO> queryUserGroupLabels(@ZZHeader ServiceHeaderEntity header,
        @NotNull Long toUid) {
        return ZZOpenScfBaseResult.buildSucc(userGroupService.queryGroupRelation(ServiceHeaderUtil.parseUid(header), toUid));
    }

    @ZZMethod(value = "addUserGroupLabel", desc = "新增分组标签", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserGroupLabelVO> addUserGroupLabel(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid AddUserGroupLabelDTO addUserGroupLabelDTO) {
        UserGroupLabelVO labelVO =
            userGroupService.addUserGroupLabel(ServiceHeaderUtil.parseUid(header), addUserGroupLabelDTO.getLabelName());
        return ZZOpenScfBaseResult.buildSucc(labelVO);
    }

    @ZZMethod(value = "delUserGroupLabels", desc = "删除分组标签", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> delUserGroupLabels(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid DelUserGroupLabelDTO delUserGroupLabelDTO) {
        userGroupService.delUserGroupLabels(ServiceHeaderUtil.parseUid(header), delUserGroupLabelDTO.getDelLabelIds());
        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @ZZMethod(value = "relateUserGroupLabels", desc = "分组标签关联到用户", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> relateUserGroupLabels(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid RelateUserGroupLabelDTO relateUserGroupLabelDTO) {
        Long fromUid = ServiceHeaderUtil.parseUid(header);
        try (ZZEtcdLock lock = lockHelper.getLock(String.format("relateUserGroupLabels_%s_%s", fromUid, relateUserGroupLabelDTO.getToUid()))) {
            if (!lock.tryLock()) {
                log.info("获取锁失败");
                return ZZOpenScfBaseResult.buildErr(ExceptionMsgEnum.COMMON_ERROR.getCode(), ExceptionMsgEnum.COMMON_ERROR.getErrorMsg());
            }
            userGroupService.relateUserGroupLabels(fromUid, relateUserGroupLabelDTO);
        }
        return ZZOpenScfBaseResult.buildSucc(true);
    }
}
